package com.fly.controller;

import com.fly.bean.*;
import com.fly.service.FixExecuteService;
import com.fly.util.JWTUtil;
import com.fly.util.Msg;
import com.fly.util.Page;
import com.fly.vo.FixAllMassage_lmg;
import com.fly.vo.FixPageVO_lmg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/fix")
public class FixExecuteController {

    @Autowired
    FixExecuteService fixExecuteService;


    /**
     * 查询所有fix会根据不同权限查
     * @param page
     * @return
     */

    @RequestMapping("/getallfix")
    @ResponseBody
    public Msg getAllFixTask(Page page,HttpServletRequest request){

        String token = request.getHeader("Authorization");

        Users users = JWTUtil.unsign(token,Users.class);

        if (users==null){
            Msg m = new Msg();
            m.setCode(201);
            m.setMsg("权限获取失败");
            return m;
        }
//        Users users = new Users();
//        users.setUserId(10000031);

        users = fixExecuteService.getUserByUserId(users.getUserId());
        FixPageVO_lmg fixes = null;
        if (users.getRole().getRoleId()<=2){
            fixes = fixExecuteService.getAllFixTask(page);
        }else {
            fixes = fixExecuteService.getAllFixTaskUser(page,users);
        }


        return Msg.success().add("fix",fixes.getFixes()).add("page",fixes.getPage());
    }

    /**
     * 根据用户获取消缺
     * @param page
     *  user
     * @return
     */
    @RequestMapping("/getallfixuser")
    @ResponseBody
    public Msg getAllFixTaskUser(Page page,HttpServletRequest request){



        String token = request.getHeader("Authorization");

        Users user = JWTUtil.unsign(token,Users.class);

        if (user==null){
            Msg m = new Msg();
            m.setCode(201);
            m.setMsg("权限获取失败");
            return m;
        }

//        //测试
//        user.setUserId(10000031);

        FixPageVO_lmg fixes = fixExecuteService.getAllFixTaskUser(page,user);

        return Msg.success().add("fix",fixes.getFixes()).add("page",fixes.getPage());
    }

    /**
     * 根据条件查询
     * @return
     */
    @RequestMapping("/getFixByConditions")
    @ResponseBody
    public Msg getFixByConditions(Page page,WorkForm workForm, Task task,Fix fix,Users users, FixPageVO_lmg fixPageVO,HttpServletRequest request){
//                                  @RequestParam("taskNo") String taskNo,
//                                  @RequestParam("workformid") int  workformid,
//                                  @RequestParam("userName") String userName,
//                                  @RequestParam("starttime") Date starttime,
//                                  @RequestParam("endtime") Date endtime){


        String token = request.getHeader("Authorization");

        Users user = JWTUtil.unsign(token,Users.class);

        if (user==null){
            Msg m = new Msg();
            m.setCode(201);
            m.setMsg("权限获取失败");
            return m;
        }
//        测试
//        Users user = new Users();
//        user.setUserId(10000031);

        //设置参数
        if (users.getUserName()!=null) {
            users.setUserName("%" + users.getUserName() + "%");
        }
        task.setUsers(users);
        if (task.getTaskNo()!=null) {
            task.setTaskNo("%" + task.getTaskNo() + "%");
        }
        fix.setWorkForm(workForm);
        fix.setTask(task);
        fixPageVO.setPage(page);
        fixPageVO.setFix(fix);


//user 为当前用户      users为查询条件
        user = fixExecuteService.getUserByUserId(user.getUserId());
        FixPageVO_lmg fixes = null;
        if (user.getRole().getRoleId()<=2){
            fixes = fixExecuteService.getFixByConditions(fixPageVO);
        }else {
            fixes = fixExecuteService.getFixByConditionsUser(fixPageVO,user);
        }

        return Msg.success().add("fix",fixes.getFixes()).add("page",fixes.getPage());
    }

    /**
     * 获取消缺任务回执的杆塔列表
     */

    @RequestMapping(value = "/getfixpolebyfixid",method = RequestMethod.GET)
    @ResponseBody
    public Msg getFixPoleByTaskId(Fix fix){

        //获取消缺任务及回执的相关信息
        Fix fix1 = fixExecuteService.getFixPoleByFixId(fix);

        //获取任务下消缺的人员
        List<Users> users = fixExecuteService.getUserByTaskId(fix1.getTask().getTaskId());

        //获取消缺任务分配杆塔的信息
        List<PoleFixRelation> poles = fixExecuteService.getPoleByFixId(fix1.getFixId());
        List<Circuitry> circuitries = new ArrayList<>();
        for (int i = 0 ;i<poles.size();i++) {
            Circuitry circuitrie = fixExecuteService.getCircuitryByPoleId(poles.get(i).getDamageRecord().getPole().getPoleId());
            circuitries.add(circuitrie);
        }
        return Msg.success().add("fix",fix1).add("user",users).add("poles",poles).add("circuitries",circuitries);
    }

    /**
     * 获取workform所有信息
     */
    @RequestMapping("/getallworkform")
    @ResponseBody
    public Msg getAllWorkForm(){
        List<WorkForm> workForms = fixExecuteService.getAllWorkForm();
        return Msg.success().add("workForms",workForms);
    }

    /**
     * 获取所有任务状态
     */
    @RequestMapping("/getalltaskstate")
    @ResponseBody
    public Msg getAllSysProValue(){
        String str = "TASK_STATUS";
        //根据配置code获取状态列表
        List<SystemPropertiesValue> taskState = fixExecuteService.getAllSysProValue(str);
        return Msg.success().add("taskstate",taskState);
    }
    /**
     * 获取所有缺陷类型
     */
    @RequestMapping("/getalldefects")
    @ResponseBody
    public Msg getAllDefects(){
        List<Defects> defects = fixExecuteService.getAllDefects();
        return Msg.success().add("defects",defects);
    }
    /**
     * 获取所有缺陷级别
     */
    @RequestMapping("/getallfixlevel")
    @ResponseBody
    public Msg getAllFixLevel(){
        List<String> strings = new ArrayList<>();
        strings.add("一般");
        strings.add("严重");
        strings.add("紧急");

        return Msg.success().add("fixlevel",strings);
    }

    /**
     * 回执录入  更新fix表记录
     */
        @RequestMapping("/submitfix")
    @ResponseBody
    public Msg updateFixById(Fix fix){
        int f = fixExecuteService.updateFixById(fix);
        return Msg.success().add("finish",f);
    }

    /**
     * 执行任务
     */
    @RequestMapping("/executetask")
    @ResponseBody
    public Msg executeTask(Fix fix, /*Users users,*/ HttpServletRequest httpRequest){

        String token = httpRequest.getHeader("Authorization");

        Users users = JWTUtil.unsign(token,Users.class);

        if (users==null){
            Msg m = new Msg();
            m.setCode(201);
            m.setMsg("权限获取失败");
            return m;
        }

        Fix fix1 = fixExecuteService.getFixPoleByFixId(fix);
        List<Users> users1 = fixExecuteService.getUserByTaskId(fix1.getTask().getTaskId());
        boolean f = false;
        for (int i = 0 ; i<users1.size();i++){
            int c = users.getUserId();
            int b = users1.get(i).getUserId();
            if (b == c){
                f = true;
            }
        }
        int flag = 0;
        if (f){
            flag = fixExecuteService.updateFixTaskStateById(fix1.getTask().getTaskId());
        }else {
            Msg msg = new Msg();
            msg.setMsg("您没有派到此本任务");
            msg.setCode(201);
            return  msg;
        }


        return Msg.success().add("finish",flag);
    }

    /**
         * 消缺查询
     */
    @RequestMapping("/selectallfix")
    @ResponseBody
    public Msg selectFixByUserId(Page page,/*Users user,*/FixPageVO_lmg fixPageVO,HttpServletRequest httpRequest){

         String token = httpRequest.getHeader("Authorization");

        Users user = JWTUtil.unsign(token,Users.class);

        if (user==null){
            Msg m = new Msg();
            m.setCode(201);
            m.setMsg("权限获取失败");
            return m;
        }
        if("".equals(fixPageVO.getDefectsLevel())){
            fixPageVO.setDefectsLevel(null);
        }

        //获取用户是否有制定任务的消缺任务的权限
        int f = fixExecuteService.getUserCreateFixByUserId(user.getUserId());
        List<Fix> fixes = null;
        List<FixAllMassage_lmg> fixsmsg = new ArrayList<>();
        if (f>0){
            //查看所有的消缺信息
            fixsmsg = fixExecuteService.selectFixAllMassage(page,fixPageVO);
        }else {
            //查看自己的消缺信息
            fixsmsg = fixExecuteService.selectFixMassageByCon(page,user,fixPageVO);
        }

        for(int i = 0; i<fixsmsg.size() ; i++){
            Task t = fixExecuteService.getCreateUserByTaskId(fixsmsg.get(i).getTaskId());
            fixsmsg.get(i).setCreateUserName(t.getUsers().getUserName());
            fixsmsg.get(i).setCreateDate(t.getCreateDate());
        }

        return Msg.success().add("fixs",fixsmsg).add("page",page);
    }

    /**
     * 获取所有的缺陷查询信息  无分页
     * @param user
     * @param fixPageVO
     * @return
     */
    @RequestMapping("/selectallfixExl")
    @ResponseBody
    public Msg selectFixByUserIdExl(Users user,FixPageVO_lmg fixPageVO) {
        int f = fixExecuteService.getUserCreateFixByUserId(user.getUserId());
        List<Fix> fixes = null;
        List<FixAllMassage_lmg> fixsmsg = new ArrayList<>();
        if (f>0){
            //查看所有的消缺信息
            fixsmsg = fixExecuteService.selectFixAllMassageExl(fixPageVO);
        }else {
            //查看自己的消缺信息
            fixsmsg = fixExecuteService.selectFixMassageByConExl(user,fixPageVO);
        }

        for(int i = 0; i<fixsmsg.size() ; i++){
            Task t = fixExecuteService.getCreateUserByTaskId(fixsmsg.get(i).getTaskId());
            fixsmsg.get(i).setCreateUserName(t.getUsers().getUserName());
            fixsmsg.get(i).setCreateDate(t.getCreateDate());
        }

        return Msg.success().add("fixs",fixsmsg);
    }

}
